package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.UmsAdmin;
import com.itheima.domain.UmsAdminRoleRelation;
import com.itheima.domain.UmsRole;
import com.itheima.mapper.UmsAdminMapper;
import com.itheima.service.UmsAdminRoleRelationService;
import com.itheima.service.UmsAdminService;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import com.itheima.service.UmsRoleService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author zhuguo
 * @since 2022-11-10
 */
@DubboService
public class UmsAdminServiceImpl extends ServiceImpl<UmsAdminMapper, UmsAdmin> implements UmsAdminService {
    @Autowired
    UmsAdminMapper umsAdminMapper;
    @Autowired
    UmsAdminRoleRelationService umsAdminRoleRelationService;
    @Autowired
    UmsRoleService umsRoleService;

    @Override
    public UmsAdmin findUserByName(String userName) {
        LambdaQueryWrapper<UmsAdmin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UmsAdmin::getUsername,userName);
        UmsAdmin user = umsAdminMapper.selectOne(lambdaQueryWrapper);
        return user;
    }

    @Override
    public Page<UmsAdmin> getAdminByPage(Integer pageNum, Integer pageSize, String keyword) {
        LambdaQueryWrapper<UmsAdmin> lqw = new LambdaQueryWrapper<>();
        Page<UmsAdmin> page = new Page<>(pageNum, pageSize);
        lqw.like(StringUtils.isNotEmpty(keyword), UmsAdmin::getUsername, keyword).or()
                .like(StringUtils.isNotEmpty(keyword),UmsAdmin::getNickName, keyword);
        return umsAdminMapper.selectPage(page, lqw);
    }

    @Override
    public void remove(Integer id) {
        umsAdminMapper.deleteById(id);
    }

    @Override
    public void update(UmsAdmin umsAdmin) {
        umsAdminMapper.updateById(umsAdmin);
    }

    @Override
    public void updateStatusById(Integer id, Integer status) {
        umsAdminMapper.updateStatusById(id,status);
    }

    @Override
    public UmsAdmin getAdminById(Long userId) {
        return umsAdminMapper.selectById(userId);
    }

    @Override
    public void updatePasswordByUsername(UmsAdmin umsAdmin) {
        umsAdminMapper.updatePasswordByUsername(umsAdmin.getUsername(), umsAdmin.getPassword());
    }



    @Override
    public String findRolesById(Long id) {
        LambdaQueryWrapper<UmsAdminRoleRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsAdminRoleRelation::getAdminId,id);
        UmsAdminRoleRelation umsAdminRoleRelation = umsAdminRoleRelationService.getOne(queryWrapper);
        UmsRole role = umsRoleService.getById(umsAdminRoleRelation.getRoleId());
        return role.getName();
    }
}
